package com.sdrt.lums.tp.message;

import com.alibaba.fastjson2.JSONObject;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Slf4j
@CrossOrigin
@ResponseBody
@Controller
public class MessageHandler {


    @GetMapping(value = "/health")
    public String healthTest() {
        return "success";
    }


    @RequestMapping(value = "/message_handler", method = {RequestMethod.POST, RequestMethod.GET})
    public String handler(@RequestParam("filedata") MultipartFile file) {
        //20251028095752_鲁Q9Q85H.jpg
        log.info("文件：{} {}", file.getOriginalFilename(), file.getSize());
        String originalName = file.getOriginalFilename();
        String carPlate = StringUtils.hasLength(originalName)?originalName.split("_")[1].split("\\.")[0]: null;
        if (SubscribeCar.isQuantitySubscribe(carPlate)) {
            File file1 = new File("/home/" + file.getOriginalFilename());
            try {
                file.transferTo(file1);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return JSONObject.toJSONString(new JSONObject());
    }

}
